#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<unistd.h>
#include<cstdlib>
#include<pthread.h>
const int threadnum = 5;
class Task
{
    public:
    Task()
    {}
    void setData(int x,int y)
    {
        datax = x;
        datay = y;
    }
    int Excute()
    {
        return datax+datay;
    }
    ~Task()
    {}
    private:
    int datax;
    int datay;
};
class ThreadData : public Task
{
    public:
    ThreadData(int x,int y,const char* threadname)
    :_threadname(threadname)
    {
        _t.setData(x,y);
    }
    std::string threadname()
    {
        return _threadname;
    }
    int run()
    {
        return _t.Excute();
    }
    private:
    std:: string _threadname;
    Task _t;
};

class Result
{
    public:
    Result()
    {}
    ~Result()
    {}
    Result(int result,const std::string threadname)
    {
        _result = result;
        _threadname = threadname;
    }
    void Print()
    {
        std::cout<<_threadname<<":"<<_result<<std::endl;
    }
    private:
    int _result;
    std::string _threadname;
};

void *hadnlerTask(void *agrs)
{
    ThreadData* td = static_cast<ThreadData *>(agrs);
    std::string name = td->threadname();

}
//多线程创建
//线程传参和返回值，可以传递级别信息，也可以传递其他对象（包括自己定义的）
int main()
{
std::vector<pthread_t> threads;
for(int i = 0;i<threadnum;i+1)
{
        char threadname[64];
        snprintf(threadname,64,"Thread-%d",i+1);
        ThreadData *td = new ThreadData(10,20,threadname);
        pthread_t tid;
        pthread_create(&tid,nullptr,hadnlerTask,td);
        threads.push_back(tid);
         
}
std::vector<Result*> result_set;
void *ret = nullptr;
for(auto &tid :threads)
{
    pthread_join(tid,&ret);
    result_set.push_back((Result*)ret);
    for(auto&res :result_set)
    {
        res->Print();
        delete res;
    }
}
    return 0;
}



//同一个进程内的线程，大部分资源都是共享的，地址空间是共享的
// int g_val =100;
// std::string ToHex(pthread_t tid)
// {
//     char id[64];
//     snprintf(id,sizeof(id),"0x%lx",tid);
//     return id;
// }
// //线程退出：
// //代码跑完，结果正确
// //代码跑完，结果不正确
// //出异常  多线程中，任何一个线程出现异常（div 0 野指针）都会导致整个进程退出
// //多线程往往健壮性不好
// void *threadrun(void *args)
// {
//     std::string threadname = (char*)args;
//     int cnt = 5;
//     while (cnt)
//     {
//         printf("new thread,g_val%d:,&g_val:%p",g_val,&g_val);
//         std::cout<<threadname<<"is running:"<<cnt<<",pid"
//         <<getpid()<<"mythread id: "<<ToHex(pthread_self())
//         <<"g_val"<<g_val<<"&g_val: "<<&g_val<<std::endl;
//         g_val++;
//         sleep(1);
//         int *p = nullptr;
//         *p =100;
//         cnt--;
//     }
//     //线程结束
//    // pthread_exit();
//     pthread_exit((void*)123);
//     exit(10);//不能使用exit终止线程，因为它是终止进程的
//     return (void*)123;
    
// }
// //主线程退出 == 进程退出==所有线程都要推出
// //1：需要main thread最后推出
// //2:线程也被要"wait",要不然会产生类似进程那里的内存泄漏问题

// int main()
// {
//     //线程创建
//     pthread_t tid;
//     pthread_create(&tid,nullptr,threadrun,(void*)"thread-1");
//     //在主线程中 保证新线程已经启动
//     sleep(2);
//     //向指定线程发送取消请求
//     pthread_cancel(tid);
//     void *ret = nullptr;
//     //2:新线程和主线程两个线程，谁先运行不确定，由调度器决定
//     int cnt = 10;
//     while (true)
//     {
//      std::cout<<"main thread is running"<<cnt<<",pid:"<<getpid()
//      <<"new thread id: "<<ToHex(tid)<<" "<<"main threead id : "
//      <<ToHex(pthread_self())<<"g_val:"<<g_val<<"&g_val:"<<&g_val<<std::endl;
//      printf("main thread ,g_val:%d,&g_val: %p\n",g_val,&g_val);
//      sleep(1);
//      cnt;   
//     }
//     int n = pthread_join(tid,&ret);
//     std::cout<<"main thread quit ,n = "<<n<<"main thread get a ret:"
//     <<(long long)ret<<std::endl;
//     return 0;
// }